<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试批量删除API</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 5px;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-test1 {
            background: #4CAF50;
            color: white;
        }
        .btn-test2 {
            background: #f44336;
            color: white;
        }
        .result {
            margin-top: 15px;
            padding: 10px;
            border-radius: 5px;
            font-family: monospace;
            font-size: 12px;
            white-space: pre-wrap;
        }
        .success {
            background: #dff0d8;
            color: #3c763d;
        }
        .error {
            background: #f2dede;
            color: #a94442;
        }
        .info {
            background: #d9edf7;
            color: #31708f;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 批量删除API测试工具</h1>
        
        <div class="test-section">
            <h3>测试1: 批量解决 (bulk_resolve)</h3>
            <p>URL: <code>POST /api/audit-logs/bulk_resolve/</code></p>
            <button class="btn-test1" onclick="testBulkResolve()">测试批量解决</button>
            <div id="result1" class="result" style="display:none;"></div>
        </div>

        <div class="test-section">
            <h3>测试2: 批量删除 (bulk_delete)</h3>
            <p>URL: <code>POST /api/audit-logs/bulk_delete/</code></p>
            <button class="btn-test2" onclick="testBulkDelete()">测试批量删除</button>
            <div id="result2" class="result" style="display:none;"></div>
        </div>

        <div class="test-section">
            <h3>说明</h3>
            <ul>
                <li><strong>401</strong> - 未授权（正常，说明URL正确）✅</li>
                <li><strong>400</strong> - 参数错误（正常，因为ids为空）✅</li>
                <li><strong>405</strong> - 方法不允许（URL错误）❌</li>
                <li><strong>404</strong> - 路由不存在❌</li>
            </ul>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8000/api';
        const TOKEN = localStorage.getItem('token') || '';

        async function testBulkResolve() {
            const resultDiv = document.getElementById('result1');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result info';
            resultDiv.textContent = '测试中...';

            try {
                const response = await fetch(`${API_BASE}/audit-logs/bulk_resolve/`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Token ${TOKEN}`
                    },
                    body: JSON.stringify({ ids: [] })
                });

                const data = await response.json();
                
                if (response.status === 401) {
                    resultDiv.className = 'result success';
                    resultDiv.textContent = `✅ 测试通过！\n状态码: ${response.status}\nURL正确，但需要登录\n响应: ${JSON.stringify(data, null, 2)}`;
                } else if (response.status === 400) {
                    resultDiv.className = 'result success';
                    resultDiv.textContent = `✅ 测试通过！\n状态码: ${response.status}\n参数错误（正常）\n响应: ${JSON.stringify(data, null, 2)}`;
                } else if (response.status === 405) {
                    resultDiv.className = 'result error';
                    resultDiv.textContent = `❌ URL错误！\n状态码: 405\nHTTP方法不允许\n响应: ${JSON.stringify(data, null, 2)}`;
                } else {
                    resultDiv.className = 'result info';
                    resultDiv.textContent = `状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`;
                }
            } catch (error) {
                resultDiv.className = 'result error';
                resultDiv.textContent = `❌ 请求失败: ${error.message}`;
            }
        }

        async function testBulkDelete() {
            const resultDiv = document.getElementById('result2');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result info';
            resultDiv.textContent = '测试中...';

            try {
                const response = await fetch(`${API_BASE}/audit-logs/bulk_delete/`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Token ${TOKEN}`
                    },
                    body: JSON.stringify({ ids: [] })
                });

                const data = await response.json();
                
                if (response.status === 401) {
                    resultDiv.className = 'result success';
                    resultDiv.textContent = `✅ 测试通过！\n状态码: ${response.status}\nURL正确，但需要登录\n响应: ${JSON.stringify(data, null, 2)}`;
                } else if (response.status === 400) {
                    resultDiv.className = 'result success';
                    resultDiv.textContent = `✅ 测试通过！\n状态码: ${response.status}\n参数错误（正常）\n响应: ${JSON.stringify(data, null, 2)}`;
                } else if (response.status === 405) {
                    resultDiv.className = 'result error';
                    resultDiv.textContent = `❌ URL错误！\n状态码: 405\nHTTP方法不允许\n\n可能的原因:\n1. URL应该是 bulk_delete 而不是 bulk-delete\n2. 前端代码未更新\n3. 浏览器缓存\n\n响应: ${JSON.stringify(data, null, 2)}`;
                } else {
                    resultDiv.className = 'result info';
                    resultDiv.textContent = `状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`;
                }
            } catch (error) {
                resultDiv.className = 'result error';
                resultDiv.textContent = `❌ 请求失败: ${error.message}`;
            }
        }

        // 页面加载时显示token状态
        window.onload = function() {
            if (!TOKEN) {
                alert('⚠️ 未检测到登录Token\n\n请先:\n1. 打开 http://localhost:8080\n2. 登录系统\n3. 再打开此测试页面');
            }
        };
    </script>
</body>
</html>
